package com.wbk.lotterysystemsystemservice.service.serviceImpl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.exception.CosServiceException;
import com.qcloud.cos.http.HttpMethodName;
import com.qcloud.cos.model.GeneratePresignedUrlRequest;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import com.wbk.lotterysystemsystemservice.service.UploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import result.Result;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Date;

@Slf4j
@Service
public class UploadServiceImpl implements UploadService {

    @Value("${cos_secretId}")
    private String cos_secretId;

    @Value("${cos_secretKey}")
    private String cos_secretKey;

    @Value("${cos_region}")
    private String cos_region;

    @Value("${bucketName}")
    private String bucketName;

    @Override
    public Result<String> uploadImag(Long userNo, MultipartFile file) {
        //COS证书
        COSCredentials cred = new BasicCOSCredentials(cos_secretId, cos_secretKey);
        //设置bucket区域
        Region region = new Region(cos_region);
        ClientConfig clientConfig = new ClientConfig(region);
        //生成COS客户端
        COSClient cosClient = new COSClient(cred, clientConfig);
        //目录+图片名
        String key = userNo + "/" + DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".png";
        String result = null;
        try {
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file.getInputStream(), new ObjectMetadata());
            //上传
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
            //返回上传后图片的url地址
            GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, key, HttpMethodName.GET);
            URL url = cosClient.generatePresignedUrl(req);
            result = url.toString().split("\\?")[0];
        } catch (IOException e) {
            return new Result<>(100, "IO异常", e.getMessage());
        } catch (CosServiceException e) {
            return new Result<>(100, "服务端异常", e.getMessage());
        } catch (CosClientException e) {
            return new Result<>(100, "客户端异常", e.getMessage());
        } finally {
            //必须及时关闭COS客户端
            cosClient.shutdown();
        }
        return new Result<>(200, "上传成功", result);
    }

    @Override
    public String uploadQrCode(Long storeUserNo, File file) {
        //COS证书
        COSCredentials cred = new BasicCOSCredentials(cos_secretId, cos_secretKey);
        //设置bucket区域
        Region region = new Region(cos_region);
        ClientConfig clientConfig = new ClientConfig(region);
        //生成COS客户端
        COSClient cosClient = new COSClient(cred, clientConfig);
        //目录+图片名
//        String key = "head_imag/" + userNo + ".png" ;
        String key = storeUserNo + "/" +DateUtil.format(new Date(),"yyyyMMddHHmmss")+RandomUtil.randomNumbers(3)+ "qrCode.png";
        String result = null;
        try {
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, file);
            //上传
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
            //返回上传后图片的url地址
            GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucketName, key, HttpMethodName.GET);
            URL url = cosClient.generatePresignedUrl(req);
            result = url.toString().split("\\?")[0];
        } catch (CosServiceException e) {
            log.error("服务端异常", e);
            return null;
        } catch (CosClientException e) {
            log.error("客户端异常", e);
            return null;
        } finally {
            //必须及时关闭COS客户端
            cosClient.shutdown();
        }
        return result;
    }
}
